<template>
  <div class="drawer-item">
    <el-switch
      :value="userForm.storagePriceType"
      active-color="#ff4949"
      inactive-color="#1890ff"
      :active-text="$t('userInfo.custom')"
      :inactive-text="$t('userInfo.default')"
      :inactive-value="1"
      :active-value="2"
      @change="setDefault"
      class="mgb20"
    >
    </el-switch>

    <div>
      <el-radio-group v-model="currentWarehouseId" v-if="warehouseList.length > 1">
        <el-radio-button
          v-for="item in warehouseList"
          :key="item.id"
          :label="item.id"
        >{{item.warehouseName}}</el-radio-button>
      </el-radio-group>
    </div>

    <div>
      <div class="label-title">
        {{$t('免仓期')}}：
      </div>
      <el-input-number
        v-model="currentWarehouseSetting.freeStorageDay"
        autocomplete="off"
        :precision="0"
        :min="0"
        @change="changeFormValue"
      ></el-input-number>
    </div>

    <div class="label-title">{{$t('仓储单价（一件代发）')}}：</div>
    <storage-weight-range :weight-range-arr="currentWarehouseSetting.stockNumRangeList" type="stock_num" :only-price="true" @changeValue="changeFormValue" weight-type="goods" :cal-type="currentWarehouseSetting.rangeTypeStockNum"></storage-weight-range>
    <div class="label-title">{{$t('仓储单价（中转送仓）')}}：</div>
    <storage-weight-range :weight-range-arr="currentWarehouseSetting.middleStockNumRangeList" type="middle_num" :only-price="true" @changeValue="changeFormValue" weight-type="box" :cal-type="currentWarehouseSetting.rangeTypeMiddleStockNum"></storage-weight-range>
    <template>
      <div class="label-title">仓储单价（快出库存）：</div>
      <storage-weight-range :weight-range-arr="currentWarehouseSetting.fullStockNumRangeList" type="full_num" :only-price="true" @changeValue="changeFormValue" weight-type="goods" :cal-type="currentWarehouseSetting.rangeTypeFullStockNum"></storage-weight-range>
    </template>

  </div>
</template>

<script>
import GoodsSizePriceMixin from "@/views/setting/mixin/GoodsSizePriceMixin";
import {deepCopy} from "@/utils";
import TransferFeeListMixin from "@/views/setting/mixin/TransferFeeListMixin";
import MiddlePriceMixin from "@/views/setting/mixin/MiddlePriceMixin";
import ShipmentPriceMixin from "@/views/setting/mixin/ShipmentPriceMixin";
import StorageWeightRange from "@/views/warehouse/components/StorageWeightRange.vue";

export default {
  name: 'StoragePriceDrawerItem',
  components: {StorageWeightRange},
  props: {
    userSetting: {
      type: Object,
    },
    userForm: {
      type: Object,
    },
    // 用户的仓库
    warehouseList: {
      type: Array,
      required: true,
    },
    setting: {
      type: Object,
    }
  },
  watch: {
    warehouseList(val){
      // console.log(val)
    },
    warehouseStorageFeeWeightRange: {
      handler: (newVal, oldVal) => {

      } ,
      deep: true,
    },
  },
  computed: {
    warehouseStorageFeeWeightRange(){
      return this.userForm.warehouseStorageFeeWeightRange
    },
    currentWarehouseSetting(){
      return this.userForm.warehouseStorageFeeWeightRange.find(item=> item.warehouseId === this.currentWarehouseId)
    },
    stockNumRangeList(){
      if(!this.currentWarehouseSetting){
        return null
      }
      return this.currentWarehouseSetting.stockNumRangeList
    },
    middleStockNumRangeList(){
      if(!this.currentWarehouseSetting){
        return null
      }
      return this.currentWarehouseSetting.middleStockNumRangeList
    },
    fullStockNumRangeList(){
      if(!this.currentWarehouseSetting){
        return null
      }
      return this.currentWarehouseSetting.fullStockNumRangeList
    }
  },
  created() {
    this.warehouseList.some(item=>{
      this.currentWarehouseId = item.id
      return true
    })

  },
  data(){
    return {
      currentWarehouseId: '', // 当前选择的仓库
    }
  },
  methods: {
    changeFormValue(){
      this.userForm.storagePriceType = 2;
    },
    // 设置大货中转默认
    setDefault(){
      if(this.userForm.storagePriceType === 1){
        this.userForm.storagePriceType = 2;
      }else{
        this.userForm.storagePriceType = 1;
      }
      this.setting.warehouseStorageFeeWeightRange.forEach(item=>{
        const currentForm = this.userForm.warehouseStorageFeeWeightRange.find(_item => _item.warehouseId === item.warehouseId)
        if(currentForm){
          currentForm.freeStorageDay = item.freeStorageDay
          currentForm.stockNumRangeList = deepCopy(item.stockNumRangeList)
          currentForm.middleStockNumRangeList = deepCopy(item.middleStockNumRangeList)
        }
      })
      // this.userForm.shipmentOneWeightPrice = deepCopy(this.userSetting.shipmentOneWeightPrice)
      // this.userForm.shipmentMiddleWeightPrice = deepCopy(this.userSetting.shipmentMiddleWeightPrice)
    },
  }
}
</script>

<style scoped lang="scss">
@import "../scss/drawer-item";
.label-title{
  display: inline-block !important;
  min-width: 100px;
}
</style>
